const moment = require('moment')
const models = require('../models')

const { User } = models
const { getPageNum } = require('../utils/tools')

exports.getInstance = function (obj) {
  return new User(obj)
}

exports.create = function (obj, callback) {
  const user = new User(obj)
  user.save(callback)
}
exports.saveById = function (id, obj, callback) {
  User.findById(id, (err, user) => {
    if (err) return callback(err)
    user.set(obj)
    user.save(callback)
  })
}
exports.updateById = function (id, obj, callback) {
  User.findByIdAndUpdate(id, obj, { new: true }, callback)
}
exports.findByEmail = function (email, callback) {
  User.findOne({ email }, callback)
}

// 用户总数
exports.userCount = function (callback) {
  User.count({ isAdmin: 0 }, callback)
}
exports.todayCount = function (callback) {
  const before = moment().format('YYYY-MM-DD 00:00:00')
  const after = moment().add(1, 'days').format('YYYY-MM-DD 00:00:00')
  User.count({ isAdmin: 0, create_at: { $gte: new Date(before), $lte: new Date(after) } }, callback)
}

exports.findByOpenId = function (openId, callback) {
  User.findOne({ openId }, callback)
}
exports.findAllByRegister = function (callback) {
  const users = User.find({ isAdmin: 0 })
  users.sort({ create_at: 1 })
  users.exec(callback)
}
exports.findById = function (id, callback) {
  User.findById(id, callback)
}
exports.findAndUpdate = function (id, obj, callback) {
  User.findByIdAndUpdate(id, obj, callback)
}
exports.findByUserName = function (userName, callback) {
  User.findOne({ userName }, callback)
}
exports.findAllByPage = function (pageNum = 1, pageSize = 10, callback) {
  pageNum = parseInt(pageNum)
  pageSize = parseInt(pageSize)
  User.count((err, count) => {
    if (err) return callback(err)
    const users = User.find({ isAdmin: 0 })
    users.sort({ create_at: -1 })
    users.skip(pageSize * (pageNum - 1))
    users.limit(pageSize)
    users.exec((err, list) => {
      if (err) return callback(err)
      callback(err, {
        users: list,
        page: pageNum,
        pageSize,
        total: count,
        count: getPageNum(count, pageSize),
      })
    })
  })
}
